package face.pay.task.order.service.impl;

import com.face.pay.AliPayConfig;
import com.face.pay.IPayConfigServer;
import com.face.pay.WxApiConfig;
import com.google.common.cache.Cache;
import face.pay.cache.LocalCache;
import face.pay.constant.OrderType;
import face.pay.dao.EnterpriseInfoDao;
import face.pay.dao.OrderInfoDao;
import face.pay.entity.OrderInfo;
import face.pay.task.order.api.ApiAliOrderQuery;
import face.pay.task.order.api.ApiWxPayQuery;
import face.pay.task.order.service.ICheckOrderPayStatusService;
import face.pay.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class CheckOrderPayStatusServiceImpl implements ICheckOrderPayStatusService {

    private Cache cache = LocalCache.cache;

    @Autowired
    private IPayConfigServer iPayConfigServer;

    @Autowired
    OrderInfoDao orderInfoDao;

    @Autowired
    ApiAliOrderQuery apiAliOrderQuery;

    @Autowired
    ApiWxPayQuery apiWxPayQuery;


    @Override
    public void checkStatus()  {

        log.info("检测订单支付状态前10-5分钟的订单");
        Map<String,Object> map =  new HashMap<>();

        Date nowDate =  new Date();

        Date endDate  = DateUtil.addMinute(nowDate,-5);

        Date startDate  = DateUtil.addMinute(nowDate,-10);

        String startTime = DateUtil.dateFormat(startDate);

        String endTime = DateUtil.dateFormat(endDate);

        map.put("startTime",startTime);

        map.put("endTime",endTime);

        List<OrderInfo> orderInfos =  this.orderInfoDao.queryNotPayOrder(map);

        log.info("总共未支付的订单"+orderInfos.size());

        for(OrderInfo orderInfo :orderInfos){

            try {
                Thread.sleep(100);
                String orderType = orderInfo.getType();

                log.info("订单类型："+orderType);

                Integer oemId = orderInfo.getOemId();

                if(OrderType.ALIPAY.equals(orderType)){
                    AliPayConfig aliPayConfig =  getAliConfig(oemId);
                    apiAliOrderQuery.orderStatusCheck(orderInfo,aliPayConfig);
                }else {
                    WxApiConfig wxApiConfig  = getWxConfig(oemId);
                    apiWxPayQuery.orderStatusCheck(orderInfo,wxApiConfig);
                }

            }catch (Exception e){
                log.error("查询订单支付状态失败",e);
            }

        }


    }

    private WxApiConfig getWxConfig(Integer oemId){

        String key = "WX_CONFIG_"+oemId;

       Object o =  cache.getIfPresent(key);

       if(o==null){
           WxApiConfig wxApiConfig =    iPayConfigServer.getConfig(oemId);
           cache.put(key,wxApiConfig);
          return wxApiConfig;
       }
       return  (WxApiConfig) o;
    }


    private AliPayConfig getAliConfig(Integer oemId){

        String key = "ALI_CONFIG_"+oemId;

        Object o =  cache.getIfPresent(key);

        if(o==null){
            AliPayConfig aliPayConfig =    iPayConfigServer.getAliConfig(oemId);
            cache.put(key,aliPayConfig);
            return aliPayConfig;
        }
        return  (AliPayConfig) o;
    }
}
